package com.grape.juc.safe.deadlock;

/**
 * 描述:死锁代码
 * @author: myx
 * @date: 2019-05-05
 * Copyright © 2019-grape. All rights reserved.
 */
public class DeadLockTest {
    public static void main(String[] args) {
        ThreadDead threadDead = new ThreadDead();
        Thread t1 = new Thread(threadDead, "窗口1");
        Thread t2 = new Thread(threadDead, "窗口2");
        t1.start();
        t2.start();
    }
}

/**
 * 描述:死锁线程模拟
 * @author: myx
 * @date: 2019-05-05
 * Copyright © 2019-grape. All rights reserved.
 */
class ThreadDead implements Runnable{
    private int trainCount = 100;
    private Object oj = new Object();

    @Override
    public void run() {
        sale();
    }

    /**
     * 同步方法
     */
    public synchronized void sale() {
        synchronized (oj) {
            try {Thread.sleep(10);} catch (Exception e) {}
            if (trainCount > 0) {
                System.out.println(Thread.currentThread().getName() + "," + "出售第" + (100 - trainCount + 1) + "票");
                trainCount--;
            }
        }
    }
}
